package com.hollycrm.unicom.sheet.business.logic.hibernate;

import org.springframework.orm.hibernate.HibernateTemplate;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;
import com.hollycrm.springframework.factory.DefaultBeanFactory;
import com.hollycrm.springframework.util.IdentifierGenerator;
import com.hollycrm.unicom.security.AuthToken;
import com.hollycrm.unicom.sheet.Constants;
import com.hollycrm.unicom.sheet.domain.ConfirmSheet;
import com.hollycrm.unicom.sheet.domain.DealSheet;
import com.hollycrm.unicom.sheet.domain.MainSheet;
import com.hollycrm.unicom.sheet.domain.SheetAppu;
import com.hollycrm.unicom.sheet.flow.SheetAction;
import com.hollycrm.unicom.sheet.flowsupport.ActionExecuteLogic;
import com.hollycrm.unicom.sheet.flowsupport.DoOperateContext;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: Hollycrm</p>
 * @author wjn
 * @version 1.0
 */

public class HibernateAddAppuLogic
    extends HibernateDaoSupport
    implements ActionExecuteLogic {
    public HibernateAddAppuLogic() {
    }

    public void execute(AuthToken auththToken,SheetAction action, DoOperateContext context) throws
        Exception {
        HibernateTemplate ht = getHibernateTemplate();
        String formFileName = (String) context.getInputAttr("formFileName");
        String mainSheetFlowNO = null;
        Object sheet = null;

        SheetAppu appu = (SheetAppu) context.getInputAttr(SheetAppu.class);
        appu.setSavedName(( (IdentifierGenerator) DefaultBeanFactory.
                               getBean("uuidGenerator")).generate());
        appu.setOriginName(formFileName);

        mainSheetFlowNO = updateAppuCount(ht,appu,context);
        ht.save(appu);
  //      writeFile( (FormFile) context.getInputAttr("formFile"), appu.getSavedName(),
  //                appu.getSheetFlowNO());
        context.addOutputAttr("dealType","ADD");
        context.addOutputAttr("mainSheetFlowNO", mainSheetFlowNO);
        context.addOutputAttr("operationObject", appu.getSheetFlowNO());
        context.addOutputAttr("originName",appu.getOriginName());
        context.addOutputAttr("savedName",appu.getSavedName());
        context.addOutputAttr("formFile",context.getInputAttr("formFile"));
        context.addOutputAttr("appu",appu);
    }

    private String updateAppuCount(HibernateTemplate ht,
                                   SheetAppu appu, DoOperateContext context) throws Exception {
        String flowNO = null;
        if(appu.getSheetType().equals(Constants.SHEET_TYPE_MAIN)){
            MainSheet sheet = (MainSheet)ht.load(MainSheet.class,appu.getSheetFlowNO());
            flowNO = sheet.getSheetFlowNO();
            sheet.setAppuCount(new Integer(sheet.getAppuCount().intValue()+1));
            ht.update(sheet);
            context.addOutputAttr(sheet);
        }else if(appu.getSheetType().equals(Constants.SHEET_TYPE_DEAL)){
            DealSheet sheet = (DealSheet)ht.load(DealSheet.class,appu.getSheetFlowNO());
            flowNO = sheet.getMainSheetFlowNO();
            sheet.setAppuCount(new Integer(sheet.getAppuCount().intValue()+1));
            ht.update(sheet);
            context.addOutputAttr(sheet.getMainSheet());
        }else if(appu.getSheetType().equals(Constants.SHEET_TYPE_CONFIRM)){
            ConfirmSheet sheet = (ConfirmSheet)ht.load(ConfirmSheet.class,appu.getSheetFlowNO());
            flowNO = sheet.getMainSheetFlowNO();
            sheet.setAppuCount(new Integer(sheet.getAppuCount().intValue()+1));
            ht.update(sheet);
            context.addOutputAttr(sheet.getMainSheet());
        }
        return flowNO;
    }

}
